//基础路径
var base = document.getElementById('testScript').getAttribute('base');

//是否登陆
var isLogin=document.getElementById('testScript').getAttribute('isLogin');
//课程id
var courseId = document.getElementById('testScript').getAttribute('courseId');
//章节id
var chapterId=document.getElementById('testScript').getAttribute('chapterId');

var app=new Vue({
    el:"#app",
    data(){
        return{
            //当前正在播放的章节id
            chapterId:chapterId,
            //所有的章节
            chapters:[],
            //正在播放的章节
            chapter:{}
        }
    },
    methods:{
        //获取所有章节
        getChapters:function () {
            var _t=this;
            var data={
                courseId:courseId
            };
            axios.get(base + "/chapter", {
                params: data
            }).then(function (response) {
                var result=response.data.data;
                if (result.length>0){
                    var index=-1;
                    for (var i = 0; i < result.length; i++) {
                        if (result[i].parent===0){//父章节
                            _t.chapters.push(result[i]);
                            index++;
                            //添加子章节数组
                            Vue.set(_t.chapters[index], 'nodes', []);
                            //添加父章节展开标记
                            Vue.set(_t.chapters[index], 'seen', false);

                        }else {
                            //添加子章节
                            _t.chapters[index].nodes.push(result[i]);
                        }
                    }
                }
            })
        },
        //显示或隐藏子章节
        chapterShow:function (index) {
            this.chapters[index].seen = !this.chapters[index].seen;
        },
        //加载当前章节信息
        getChapter:function (chapterId) {
            var _t=this;
            axios.get(base + "/chapter/"+chapterId)
                .then(function (response) {
                _t.chapter=response.data.data;
            });
        },
        //切换章节
        clickChapter:function (node) {
            //更新地址栏
            var title = node.title;
            var newUrl = base+"/chapter/video/"+courseId+"/"+node.id;
            history.pushState({},'',newUrl);
            document.title=title;
            this.getChapter(node.id);
        }
    },
    created(){
        //加载当前章节详细信息
        this.getChapter(chapterId);
        //加载所有章节列表
        this.getChapters();
    }
});